/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AspectMethodGUI.java
 *
 * Created on 6 Jan, 2010, 4:29:36 PM
 */
package jaspect;

import jaspect.model.AspectMethod;
import java.awt.Frame;
import java.util.Enumeration;
import java.util.Properties;

/**
 *
 * @author Arjun
 */
public class AspectMethodGUI extends javax.swing.JDialog {

    Frame jf;
    AspectDefDlg ad;
    private AspectMethod td = new AspectMethod();

    /** Creates new form AspectMethodGUI
     * @param parent
     * @param modal
     * @param ad
     */
    public AspectMethodGUI(java.awt.Frame parent, AspectDefDlg ad, boolean modal) {
        super(parent, modal);
        this.ad = ad;
        jf = parent;
        initComponents();
        for (int i = 0; i < tableArguments.getRowCount(); i++) {
            tableArguments.setValueAt("", i, 0);
            tableArguments.setValueAt("", i, 1);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tbMethodName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        buttonMAdd = new javax.swing.JButton();
        buttonMClose = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        cbAllFields = new javax.swing.JCheckBox();
        buttonReset = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableArguments = new javax.swing.JTable();
        buttonMethodBody = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(0, 204, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setText("Method Name");

        buttonMAdd.setText("Add ");
        buttonMAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonMAddActionPerformed(evt);
            }
        });

        buttonMClose.setText("Close");
        buttonMClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonMCloseActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Parameter definition:"));
        jPanel1.setLayout(new java.awt.BorderLayout());

        cbAllFields.setText("Include All Class Fields as Parameter ");
        jPanel2.add(cbAllFields);

        jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_START);

        buttonReset.setText("Reset");
        buttonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonResetActionPerformed(evt);
            }
        });

        tableArguments.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Param Name", "Param Type"
            }
        ));
        jScrollPane1.setViewportView(tableArguments);

        buttonMethodBody.setText("Method Body");
        buttonMethodBody.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonMethodBodyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(85, 85, 85)
                        .addComponent(jLabel1)
                        .addGap(29, 29, 29)
                        .addComponent(tbMethodName, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(221, Short.MAX_VALUE)
                .addComponent(buttonReset)
                .addGap(18, 18, 18)
                .addComponent(buttonMAdd)
                .addGap(18, 18, 18)
                .addComponent(buttonMClose)
                .addGap(25, 25, 25))
            .addGroup(layout.createSequentialGroup()
                .addGap(88, 88, 88)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(53, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(buttonMethodBody)
                .addContainerGap(343, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbMethodName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(buttonMethodBody)
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonMAdd)
                    .addComponent(buttonMClose)
                    .addComponent(buttonReset))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
private void save() {
     td.setMethodName(tbMethodName.getText());
        td.setAllClassFields(cbAllFields.isSelected());
        Properties ps = new Properties();
        try {
            for (int ri = 0; ri < tableArguments.getRowCount(); ri++) {
                if (tableArguments.getValueAt(ri, 0) == "" || tableArguments.getValueAt(ri, 0) == "") {
                    break;
                }
                String se = tableArguments.getValueAt(ri, 0).toString();
                String sg = tableArguments.getValueAt(ri, 1).toString();
                ps.setProperty(se, sg);
            }
        } catch (Exception ie) {
            System.out.println("Aspectmethodgui " + ie);
        }
        td.setArguments(ps);
        ad.setMethod(td);
}
    private void buttonMAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMAddActionPerformed
        // TODO add your handling code here:

        td.setMethodName(tbMethodName.getText());
        td.setAllClassFields(cbAllFields.isSelected());
        Properties ps = new Properties();
        try {
            for (int ri = 0; ri < tableArguments.getRowCount(); ri++) {
                if (tableArguments.getValueAt(ri, 0) == "" || tableArguments.getValueAt(ri, 0) == "") {
                    break;
                }
                String se = tableArguments.getValueAt(ri, 0).toString();
                String sg = tableArguments.getValueAt(ri, 1).toString();
                ps.setProperty(se, sg);
            }
        } catch (Exception ie) {
            System.out.println("Aspectmethodgui " + ie);
        }
        td.setArguments(ps);
        ad.setMethod(td);
        this.setVisible(false);

        AspectMethodGUI d = new AspectMethodGUI(jf, ad, true);
        d.setVisible(true);

    }//GEN-LAST:event_buttonMAddActionPerformed

    private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonResetActionPerformed
        clearFields();        // TODO add your handling code here:
    }//GEN-LAST:event_buttonResetActionPerformed

    private void buttonMCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMCloseActionPerformed

        this.setVisible(false);        // TODO add your handling code here:
    }//GEN-LAST:event_buttonMCloseActionPerformed


    private void buttonMethodBodyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMethodBodyActionPerformed
        // TODO add your handling code here:
        save();
        AspectMethodBodyGUI mb = new AspectMethodBodyGUI(jf,this, true);
        mb.setMethodName(td.getMethodName());

        StringBuilder sb = new StringBuilder();
        Properties ps = td.getArguments();
      
        Enumeration en = ps.keys();
        String se;
        for (int i = 0; i < ps.size(); i++) {
            se = en.nextElement().toString();
            sb.append(ps.getProperty(se));
            sb.append(" ");

            sb.append(se);
            sb.append(", ");
        }

        mb.setArgumentList(sb.toString().substring(0, sb.toString().lastIndexOf(",")));
        mb.setInit();
        mb.setVisible(true);

       
    }//GEN-LAST:event_buttonMethodBodyActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonMAdd;
    private javax.swing.JButton buttonMClose;
    private javax.swing.JButton buttonMethodBody;
    private javax.swing.JButton buttonReset;
    private javax.swing.JCheckBox cbAllFields;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableArguments;
    private javax.swing.JTextField tbMethodName;
    // End of variables declaration//GEN-END:variables


    /**
     *
     * @param s
     */
    public void setMethodBody(String s) {
        td.setMethodBody(s);
    }
    /**
     *
     * @param am
     */
    public void setMethod(AspectMethod am) {
        tbMethodName.setText(am.getMethodName());
        cbAllFields.setSelected(am.getAllClassFields());
        Properties ps = am.getArguments();
        Enumeration en = ps.keys();
        int ri;
        String se = new String();
        for (ri = 0; ri < ps.size(); ri++) {
            se = en.nextElement().toString();
            tableArguments.setValueAt(se, ri, 0);
            tableArguments.setValueAt(ps.getProperty(se), ri, 1);
        }


    }

    void clearFields() {
        tbMethodName.setText(null);
        cbAllFields.setSelected(false);
        //clear table also

        int rm = tableArguments.getRowCount();
        for (int i = 0; i < rm; i++) {

            tableArguments.setValueAt(null, i, 0);
            tableArguments.setValueAt(null, i, 1);

        }

    }

    /**
     * @return the td
     */
    public AspectMethod getTd() {
        return td;
    }

    /**
     * @param td the td to set
     */
    public void setTd(AspectMethod td) {
        this.td = td;
    }
}
